package org.example;

import java.net.URL;
import java.net.URLClassLoader;
import java.sql.*;

/**
 * @author jiangjian
 * @desc 使用Jdbc连接数据库
 * @since 2023/6/30 10:52
 */
public class JdbcConn {

    public static void informix() throws Exception {
        // 加载驱动
//        Class.forName("com.informix.jdbc.IfxDriver");
        // 用户连接信息 :DB_LOCALE=en_US.57372;CLIENT_LOCALE=en_US.57372;
        String url38 = "jdbc:informix-sqli://192.168.1.38:13883/testdb";
        String url39 = "jdbc:informix-sqli://192.168.1.39:16494/test123:DB_LOCALE=en_US.57372;CLIENT_LOCALE=en_US.57372;";
        String username = "informix";
        String password = "Newdt@cn";
        // 连接数据库
        Connection connection = DriverManager.getConnection(url39, username, password);
        //创建数据库操作对象
        Statement statement = connection.createStatement();
        // 执行sql语句，返回结果
        String sql = "SELECT DBINFO('version','full') FROM systables WHERE tabid = 1";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }

        resultSet.close();
        statement.close();
        connection.close();
    }

    public static void gaussDBOG() throws Exception {
        // 加载驱动
        URLClassLoader gsClassLoader = new URLClassLoader(new URL[]{new URL("file:" + System.getProperty("user.dir") + "/lib/gsjdbc200.jar")});
        Class.forName("com.huawei.gauss200.jdbc.Driver", true, gsClassLoader);
        // 用户连接信息
        String url = "jdbc:gaussdb://192.168.12.145:33700/postgres";
        String username = "dbmon";
        String password = "dbmon@1234";
        // 连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //创建数据库操作对象
        Statement statement = connection.createStatement();
        // 执行sql语句，返回结果
        String sql = "select pg_is_in_recovery() as role";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getBoolean(1));
        }

        resultSet.close();
        statement.close();
        connection.close();
    }

    public static void clickhouse() throws SQLException {
        String url = "jdbc:clickhouse://192.168.12.56:8123";
        String username = "default";
        String password = "Newdt@cn";
        // 连接数据库
        Connection connection = DriverManager.getConnection(url, username, password);
        //创建数据库操作对象
        Statement statement = connection.createStatement();
        // 执行sql语句，返回结果
        String sql = "select version();";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }

        resultSet.close();
        statement.close();
        connection.close();
    }

}
